package cn.xuewei.pachong;

import cn.xuewei.pachong.util.DBUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

public class Chrome {
    public static void main(String[] args) {
        System.getProperties().setProperty("webdriver.chrome.driver", "D:\\java\\ChromeDriver\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        String url = "https://tes.tmooc.cn/login/toLogin";
        driver.get(url);
//        WebElement link = chromeDriver.findElement(By.id("login_xxw"));
//        String href = link.getAttribute("href");
//
        Cookie cookie1 = new Cookie("sensorsdata2015jssdkcross", "%7B%22distinct_id%22%3A%22E_bfv6e8n%22%2C%22first_id%22%3A%221884bb80219821-083f47b54599e18-26031a51-1327104-1884bb8021a132e%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfbG9naW5faWQiOiJFX2JmdjZlOG4iLCIkaWRlbnRpdHlfY29va2llX2lkIjoiMTg4NGJiODAyMTk4MjEtMDgzZjQ3YjU0NTk5ZTE4LTI2MDMxYTUxLTEzMjcxMDQtMTg4NGJiODAyMWExMzJlIn0%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22E_bfv6e8n%22%7D%2C%22%24device_id%22%3A%221884bb80219821-083f47b54599e18-26031a51-1327104-1884bb8021a132e%22%7D");


        Cookie cookie2 = new Cookie("Hm_lvt_51179c297feac072ee8d3f66a55aa1bd", "1684897704,1684926695");


        Cookie cookie3 = new Cookie("sessionid", "20d2f17737804ab79572930296176c8d%7CE_bfv6e8n");


        Cookie cookie4 = new Cookie("sid", "A1636662255248629760");

        // 添加 Cookie 到浏览器
        driver.manage().addCookie(cookie1);
        driver.manage().addCookie(cookie2);
        driver.manage().addCookie(cookie3);
        driver.manage().addCookie(cookie4);


        // 使用登录状态访问目标网页
        driver.get("https://tes.tmooc.cn/qsexam/QSRandomPaperExam?courseId=qw2055&courseL1Id=qw2030&courseL2Id=qw2055&courseL3Id=");
//        String pageSource = driver.getPageSource();
//        System.out.println(pageSource);
//        WebElement elementList = driver.findElement(By.xpath("//div[@class='md-2018121002-xxw js-parent-box']"));
//        List<WebElement> elements = elementList.findElements(By.xpath("//div[@class='other-1221-bd']"));
//        for (WebElement dri:elements) {
//            //题目
//            WebElement questionElement = dri.findElement(By.xpath("//div[@class='tit ']/xmp"));
//            String question = questionElement.getText();
//
//            //答案解析
//           // WebElement analysisElement = dri.findElement(By.xpath("//div[@class='md-footer']"));
//            // 获取答案解析文本
//          //  String analysis = analysisElement.getText();
//            System.out.println(question);
//           // System.out.println(analysis);
//        }
       // int i = 1;
       // WebElement elementList = driver.findElement(By.xpath("//div[@class='md-2018121002-xxw js-parent-box']"));
//        List<WebElement> elements = driver.findElements(By.xpath("//div[@class='other-1221-bd']"));
//        for (WebElement dri : elements) {
//            WebElement questionElement = new WebDriverWait(driver, 10)
//                    .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='tit ']/xmp")));
//
//            // 获取题目
//            String question = questionElement.getText().trim();
//
//            // 获取选项
//            List<String> options = new ArrayList<String>();
//            List<WebElement> optionElements = dri.findElements(By.xpath("//div[@class='bd']/div[@class='form-checkbox form-checkbox-info item-listx']"));
//            for (WebElement optionElement : optionElements) {
//                String optionText = optionElement.getAttribute("textContent").trim();
//                options.add(optionText);
//            }
//
//            // 获取答案
//            WebElement answerElement = dri.findElement(By.xpath("//div[@class='md-footer']/div[@class='d1 color-28b2f7'][1]"));
//            String answer = (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].textContent;", answerElement);
//            String regex = "^【正确答案】";
//            answer = answer.replaceAll(regex, "").trim();
//            // String answer = answerElement.getText();
//
//
//            // 获取解析
//            WebElement explanationElement = dri.findElement(By.xpath("//div[@class='md-footer']/div[@class='d1 color-28b2f7'][2]"));
//            String explanation = (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].textContent;", explanationElement);
//            regex = "^【答案解析】";
//            explanation = explanation.replaceAll(regex, "").trim();
//            // String explanation = explanationElement.getText();
//
//            // 输出结果
//            System.out.println(i+++"-------------------------------------");
//            System.out.println("题目：" + question);
//            System.out.println("选项：" + options);
//            System.out.println("答案：" + answer);
//            System.out.println("解析：" + explanation);
        List<WebElement> questions = driver.findElements(By.xpath("//div[@class='tit ']/xmp"));

        // 获取所有选项信息
        List<WebElement> options = driver.findElements(By.xpath("//div[@class='form-checkbox form-checkbox-info item-listx']"));

        // 获取所有答案信息
        List<WebElement> answers = driver.findElements(By.xpath("//div[@class='d1 color-28b2f7'][1]"));

        // 获取所有解析信息
        List<WebElement> explanations = driver.findElements(By.xpath("//div[@class='d1 color-28b2f7'][2]"));

        // 输出所有题目、选项、答案和解析信息
        int j =0;
        for (int i = 0; i < questions.size(); i++) {
            WebElement question = questions.get(i);
            WebElement option = options.get(i);
            WebElement answerElement = answers.get(i);
            String answer = (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].textContent;", answerElement);
            String regex = "^【正确答案】";
            answer = answer.replaceAll(regex, "").trim();
            WebElement explanationElement = explanations.get(i);
            String explanation = (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].textContent;", explanationElement);
            regex = "【.*?】";
             explanation = explanation.replaceAll(regex, "").trim();
            System.out.println("题目：" + question.getText());
            System.out.println("选项：" + option.getText());
            System.out.println("答案：" + answer);
            System.out.println("解析：" + explanation);
            try (Connection connection = DBUtil.getConnection()) {
                String insertSQL = "INSERT INTO test_paper(question,optionA,optionB,optionC,optionD,answer,analysis) " +
                        "VALUES(?,?,?,?,?,?,?)";
                PreparedStatement ps2 = connection.prepareStatement(insertSQL);
                ps2.setString(1,question.getText());
                ps2.setString(2,options.get(j+0).getText());
                ps2.setString(3,options.get(j+1).getText());
                ps2.setString(4,options.get(j+2).getText());
                ps2.setString(5,options.get(j+3).getText());
                ps2.setString(6,answer);
                ps2.setString(7,explanation);
                ps2.executeUpdate();
                j = j+4;
            }catch (Exception e){
                System.out.println(e);
            }
        }
//        for (j = 0;j < options.size();j++) {
//            System.out.println("选项：" +options.get(j).getText());
//        }




        // 关闭浏览器
      driver.quit();
    }
}
